package cn.com.api.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiGoods;
import cn.com.api.entity.ApiShop;
import cn.com.api.service.ApiConllectionService;
import cn.com.api.service.ApiGoodsService;
import cn.com.api.service.ApiShopsService;
import cn.com.util.*;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(Utils.SYS_API + "conllection/")
public class ApiCollectionController {
	@Autowired
	private ApiGoodsService goodService;
	@Autowired
	private ApiShopsService ShopsService;
	@Autowired
	private ApiConllectionService conllectService;

	@RequestMapping("/insertConllection")
	public void insertConllection(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = request.getParameter("u_id");
		u_id = new String(u_id.getBytes("ISO-8859-1"), "utf-8");
		String sg_id1 = request.getParameter("sg_id");
		sg_id1 = new String(sg_id1.getBytes("ISO-8859-1"), "utf-8");
		String enable1 = request.getParameter("enable");
		enable1 = new String(enable1.getBytes("ISO-8859-1"), "utf-8");
		Integer shops_goods_id = Integer.parseInt(sg_id1);
		Integer type = Integer.parseInt(enable1);
		conllectService.insert(u_id, shops_goods_id, type);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	@RequestMapping("/delConllect")
	public void delConllect(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String sg_id = request.getParameter("id");
		conllectService.delconllet(sg_id);
		sg_id = new String(sg_id.getBytes("ISO-8859-1"), "utf-8");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "删除成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 搜索我收藏的商品
	@RequestMapping("/searchMyConllection")
	public void searchMyConllection(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Map<String, Object> map = new HashMap<String, Object>();
		String u_id = request.getParameter("id");
		String obj = request.getParameter("obj");

		if (obj.equals("1")) {
			List<ApiShop> datas = ShopsService.searchMyShops(u_id);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put("datas", datas);
			out.print(JsonUtil.object2json(map));
			out.close();
		} else if (obj.equals("2")) {
			List<ApiGoods> datas = goodService.searchMyGoods(u_id);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put("datas", datas);
			out.print(JsonUtil.object2json(map));
			out.close();
		}

	}
}
